//暴露的总接口 在window上创造一个对象
var root = window.player;
var index = 0;
var dataList;
var len;
var audio = root.CommandAudio;

function getData(url) {
    $.ajax({
        type: 'GET',
        url: url,
        success: function (data) {
            dataList = data;
            root.render(data[0]);
            len = data.length;
            audio.getAudio(data[0].audio)
            bindEvent();
            root.pro.renderAllTime(data[0].duration);      
        },
        error: function () {
            console.log('error')
        }
    })
}

function bindEvent() {
    $('.prev').on('click', function () {    
        // if (index == 0) {
        //     index = len - 1;
        // } else {
        //     console.log(index)
        //     index--;
        // }
        var i = root.CIndex.prev()
        audio.getAudio(dataList[i].audio);
        root.render(dataList[i]);
        root.pro.renderAllTime(dataList[i].duration); 
        if(audio.status == 'play'){
            audio.play()
        }
    })
    $('.next').on('click', function () {
        // console.log(index)
        // if (index == len-1) {
        //     index = 0;
        // } else {
        //     console.log(index)
        //     index ++ ;
        // }
        var i = root.CIndex.next();
        audio.getAudio(dataList[i].audio);
        root.render(dataList[i]);
        root.pro.renderAllTime(dataList[i].duration); 
        if(audio.status == 'play'){
            audio.play()
        }
    })
    $('.play').on('click',function(){
        if(audio.status == 'pause'){
            
            root.pro.start()
            audio.play();
            $('.play').addClass('playing');
        }else{
            root.pro.stop()
            audio.pause();
            $('.play').removeClass('playing');
        }
    })
  


}
getData('../mock/data.json')
//打包以后的路径